package com.fantasy.ui;

import java.net.SocketException;
import java.util.ArrayList;

import com.fantasy.EventListener;
import com.fantasy.R;
import com.fantasy.TaskController;
import com.fantasy.socket.SocketProcesser;
import com.fantasy.ui.WimSocketServerActivity.receiveMessage;
import com.fantasy.util.IMUtil;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ConversionActivity extends ListActivity implements OnClickListener {

	public static String TAGET_ADDRESS = "TARGET_ADDRESS";
	public static String TARGET_NAME = null;
	private static final String TAG = "ConversionActivity";
	private static final boolean DEBUG = true;

	private static final int RECEIVE_NEW_MESSAGE = 1;
	private static final int RECEIVE_NEW_IMAGE = 2;
	private static final int RECEIVE_NEW_SOUND = 3;

	private static final int MENU_ATTACH_IMAGE = 4;
	private static final int MENU_ATTACH_APK = 5;
	private static final int MENU_ATTACH_MP3 = 6;
	private static final int MENU_ATTACH_FILE = 7;
	private static final int MENU_ATTACH_VIDEO = 12;

	private static final int REQUEST_ATTACH_IMAGE = 8;
	private static final int REQUEST_ATTACH_APK = 9;
	private static final int REQUEST_ATTACH_MP3 = 10;
	private static final int REQUEST_ATTACH_FILE = 11;
	private static final int REQUEST_ATTACH_VIDEO = 13;

	private TextView mMessageBox;
	private EditText mInputMessage;
	private Button mSendBtn;

	private String mIp;
	private String mName;

	private ArrayAdapter<String> mAdapter;
	private ArrayList<String> mStrings = new ArrayList<String>();

	private TaskController mTc;
	// private SocketProcesser mProcesser;

	private receiveMessage rm;
	private MessageSpec mSpec;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case RECEIVE_NEW_MESSAGE:
				String message = (String) msg.obj;
				mAdapter.add(message);
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compose_activity);
		initActivity();
		initResource();
		setListAdapter(mAdapter);
		// registerListener();
	}

	private void initActivity() {
		Intent intent = getIntent();
		mName = intent.getStringExtra(TARGET_NAME);
		mIp = intent.getStringExtra(TAGET_ADDRESS);
		Log.d(TAG,"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@targetIp:"+mIp);
		if (DEBUG) {
			Log.d(TAG, "initActivity:Name:" + mName);
			Log.d(TAG, "IP:" + mIp);
		}
		this.setTitle(mName);
		mTc = TaskController.getInstance();
	}

	public void onClick(View v) {
		sendMessage();
	}

	private void initResource() {
		// mMessageBox = (TextView)findViewById(R.id.receive_msg);
		mInputMessage = (EditText) findViewById(R.id.input);
		mSendBtn = (Button) findViewById(R.id.send_msg);
		mSendBtn.setOnClickListener(this);
		mAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mStrings);
		/*
		 * mProcesser = new SocketProcesser(); try { mProcesser.getInstance(); }
		 * catch (SocketException e) { e.printStackTrace(); }
		 */
	}

	private void sendMessage() {
		// startSocketing();
		String message = mInputMessage.getText().toString();
		if ("".equals(message) || (null == message)) {
			Toast.makeText(this, R.string.empty_message, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		mAdapter.add(message);
		mTc.mTaskType = TaskController.TASK_SEND_MESSAGE;
		mTc.mTargetAddress = mIp;
		mTc.mMsgContent = message;
		mTc.startTask(this);
		mInputMessage.setText(null);
		// setListAdapter(mAdapter);
	}

	private class MessageEvent extends EventListener {

		@Override
		public void newMessage(String message) {
			super.newMessage(message);
			Message msg = new Message();
			msg.arg1 = RECEIVE_NEW_MESSAGE;
			msg.obj = message;
			mHandler.sendMessage(msg);
		}

	}
}
